<?php

namespace app\company\controller;

use app\admin\model\product\Listo;
use app\admin\model\product\Lists;
use app\admin\model\product\Order;
use app\common\controller\CompanyApi;
use think\Db;
use think\Request;

class Workload extends CompanyApi
{
    protected static $model = null;
    protected $noNeedRight = '*';

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\admin\model\Workload();
    }

    /**
     * 工足量排行榜
     */
    public function rankLists()
    {
        $day = date('Y-m-d', time());
        $date = $this->request->post('date', $day);
//        $type = $this->request->post('type', 1);

//        $date_type = $this->request->post('date_type', 1);
//        switch ($date_type) {
//            case 1:
//                $start = strtotime('today');
//                break;
//            case 2:
//                $start = strtotime('this week');
//                break;
//        }

        $start = strtotime($date);
        $end = $start + 24 * 60 * 60;
        $where_str = ' datetime >= ' . $start . ' and datetime < ' . $end;
        $loads_sql = "select user_id,sum(num1) as snum1,sum(num2) as snum2 from fa_workload where " . $where_str . " group by user_id";
        $loads = Db::query($loads_sql);
        $loads_data = [];
        if ($loads) {
            foreach ($loads as $key => $val) {
                $item = [
                    'id' => $val['user_id'],
                    'num' => (int)($val['snum1'] + $val['snum2'])
                ];

                $loads_data[] = $item;
            }
        }

        $num_arr = array_column($loads_data, 'num');
        array_multisort($num_arr, SORT_DESC, $loads_data);

        $data = [];
        foreach ($loads_data as $val) {
            $row = \app\admin\model\department\User::field('id,username,nickname,avatar')->find($val['id']);
            $item = [
                'username' => $row->username,
                'nickname' => $row->nickname,
                'avatar' => make_full_url($row->avatar),
                'num' => $val['num'],
            ];
            $data[] = $item;
        }

        $this->success('排行榜', $data);
    }
}